package oa.hleast.xswl.service.oms.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.mapper.oms.OmsOrderCommentReplyMapper;
import oa.hleast.xswl.pojo.domain.oms.OmsOrderComment;
import oa.hleast.xswl.pojo.domain.oms.OmsOrderCommentReply;
import oa.hleast.xswl.pojo.dto.SysUserDto;
import oa.hleast.xswl.pojo.form.OmsOrderCommentReplyForm;
import oa.hleast.xswl.service.ISysUserService;
import oa.hleast.xswl.service.oms.OmsOrderCommentReplyService;
import oa.hleast.xswl.service.oms.OmsOrderCommentService;
import org.springframework.stereotype.Service;

/**
 * @author: 302
 * @date: 2021-10-26 18:58:27
 * @description: 订单评论回复表(OmsOrderCommentReply)接口实现
 */
@AllArgsConstructor
@Slf4j
@Service("omsOrderCommentReplyService")
public class OmsOrderCommentReplyServiceImpl extends ServiceImpl<OmsOrderCommentReplyMapper, OmsOrderCommentReply> implements OmsOrderCommentReplyService {

    private final ISysUserService sysUserService;
    private final OmsOrderCommentService commentService;

    @Override
    public Boolean replyComment(OmsOrderCommentReplyForm form) {


        OmsOrderComment orderComment = commentService.getById(form.getOrderCommentId());
        SysUserDto sysUserDto = sysUserService.getSysUserDtoById(RequestUtils.getUserId());

        //  检查是否是自己店铺的评论
        Long storeId = sysUserDto.getStoreId();
        Long commentStoreId = orderComment.getStoreId();
        if (!storeId.equals(commentStoreId)) {
            throw new BizException("您只能回复自己店铺的评论");
        }

        OmsOrderCommentReply commentReply = new OmsOrderCommentReply();
        BeanUtil.copyProperties(form, commentReply);
        commentReply.setStoreId(storeId);
        commentReply.setSpuId(orderComment.getSpuId());
        commentReply.setOrderId(orderComment.getOrderId());

        boolean save = this.save(commentReply);

        return save;
    }
}
